﻿using BLL;
using Model;
using Newtonsoft.Json.Linq;
using System;
using Utility.Helper;

namespace Thksoft.Funds
{
    public partial class BANK_QRCODE : System.Web.UI.Page
    {
        private YX_BANK_QRCODE_BLL bll = new YX_BANK_QRCODE_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["TYPE"] == "API")
                {
                    var data = AjaxResult.GetPostData(this.Context);
                    if (data["Type"].NewToObject<string>() == "GET_PAGE")
                    {
                        var reson = new JObject();
                        var pageSize = data["PAGE_SIZE"].NewToObject<int>();
                        var currentPage = data["CURRENT_PAGE"].NewToObject<int>();
                        var cmyId = data["COMPANY_ID"].NewToObject<int>();
                        if (currentPage == 1)
                        {
                            reson["Count"] = bll.GetDataByCount(cmyId);
                        }
                        reson["Data"] = bll.GetDataByPage(cmyId, (currentPage - 1) * pageSize, pageSize).ToJToken();
                        AjaxResult.Success(this.Context, reson);
                        return;
                    }
                    if (data["Type"].NewToObject<string>() == "SAVE")
                    {
                        var _entity = data["Data"].NewToObject<YX_BANK_QRCODE>();
                        var entity = bll.GetInfo(_entity.ID);
                        if (entity == null)
                        {
                            AjaxResult.Reson(this.Context, 0, null, "当前数据已不存在");
                            return;
                        }
                        entity.OPEAR_ID = (int)bll.User.USER_ID;
                        entity.OPEAR_NAME = bll.User.USER_NAME;
                        entity.OPEAR_DATE = DateTime.Now;
                        entity.NAME = _entity.NAME;
                        entity.CODE = _entity.CODE;
                        entity.COMPANY_ID = _entity.COMPANY_ID;
                        bll.Update(entity);
                        AjaxResult.Success(this.Context, entity);
                        return;
                    }
                    if (data["Type"].NewToObject<string>() == "ADD")
                    {
                        var entity = data["Data"].NewToObject<YX_BANK_QRCODE>();
                        entity.OPEAR_ID = (int)bll.User.USER_ID;
                        entity.OPEAR_NAME = bll.User.USER_NAME;
                        entity.OPEAR_DATE = DateTime.Now;
                        bll.Insert(entity);
                        AjaxResult.Success(this.Context, entity);
                        return;
                    }
                    if (data["Type"].NewToObject<string>() == "GET_INFO")
                    {
                        var id = data["ID"].NewToObject<string>();
                        var entity = bll.GetInfo(id);
                        AjaxResult.Success(this.Context, entity);
                        return;
                    }
                    if (data["Type"].NewToObject<string>() == "DELETE")
                    {
                        var id = data["ID"].NewToObject<string>();
                        var entity = bll.GetInfo(id);
                        if (entity != null)
                        {
                            bll.Delete(id);
                        }
                        else
                        {
                            AjaxResult.Reson(this.Context, 0, null, "当前数据已删除");
                            return;
                        }
                        AjaxResult.Success(this.Context);
                        return;
                    }
                    return;
                }
            }
        }
    }
}